package com.atilim.cargo.model;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;

import com.atilim.cargo.util.Utility;

/**
 * Yol verisini tutar
 * 
 * @author aylin
 *
 */
@Entity
public class Road extends BaseModel {

	@ManyToOne
	private Vertex v1;

	@ManyToOne
	private Vertex v2;

	private double distance;

	public Vertex getSource () {
		return v1;
	}
	
	public Vertex getDestination () {
		return v2;
	}
	
	public double getWeight (){
		return distance;
	}
	
	public Road(Vertex v1, Vertex v2) {
		super();
		this.v1 = v1;
		this.v2 = v2;
		this.v1.addLink(this.v2);
		this.distance = Utility.distance(v1, v2);
	}
	public Road(Vertex v1, Vertex v2, double distance) {
		super();
		this.v1 = v1;
		this.v2 = v2;
		this.v1.addLink(this.v2);
		this.distance = distance;
	}


	public Vertex getV1() {
		return v1;
	}

	public void setV1(Vertex v1) {
		this.v1 = v1;
	}

	public Vertex getV2() {
		return v2;
	}

	public void setV2(Vertex v2) {
		this.v2 = v2;
	}

	@Override
	public boolean equals(Object obj) {
		Road r1 = (Road) obj;
		return ((r1.v1.equals(this.v2) && r1.v2.equals(this.v1)) || (r1.v1
				.equals(this.v1)) && r1.v2.equals(this.v2));
	}

	@Override
	public int hashCode() {
		return v1.hashCode() + v2.hashCode();
	}

	public double getDistance() {
		return distance;
	}

	public void setDistance(double distance) {
		this.distance = distance;
	}

	@Override
	public String toString() {
		return "[" + v1 + " " + v2 + " :" + distance + "] ";
	}
	
	public boolean containsVertex (Vertex vertex) {
		return  v1.equals(vertex) || v2.equals(vertex);
	}

	/**
	 * vertex dışındaki diğer nod'u döner,
	 * hiç uymuyorsa null döner
	 * @param vertex
	 * @return
	 */
	public Vertex getAccros (Vertex vertex) {
		if (v1.equals(vertex))
			return v2;
		if (v2.equals(vertex))
			return v1;
		return null;
	}

}
